/*创建我的全局通用组件*/

var Common = function () {

    // 点击按钮：出现进度条
    var handleButtons = function () {

        $('.demo-loading-btn').click(function () {
            var btn = $(this)
            btn.button('loading')
            setTimeout(function () {
                btn.button('reset')
            }, 3000)
        });

        Ladda.bind( '.mt-ladda-btn', { timeout: 2000 } );
        Ladda.bind( '.mt-ladda-btn.mt-progress-demo ', {
            callback: function( instance ) {
                var progress = 0;
                var interval = setInterval( function() {
                    progress = Math.min( progress + Math.random() * 0.1, 1 );
                    instance.setProgress( progress );

                    if( progress === 1 ) {
                        instance.stop();
                        clearInterval( interval );
                    }
                }, 200 );
            }
        } );

    }

    // 提示组件 制作 Confirm 采用 Javascript 回调函数
    var handleConfirm = function (title,subTitle,callback) {
        swal({
            title: title,
            text: subTitle,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是",
            cancelButtonText: "否",
            closeOnConfirm: true,
            closeOnCancel: true,
        }, function (isConfirm) {
            if(isConfirm&&typeof callback == 'function'){
                callback();
            }
        });
    }

    // 提示框
    var alertError = function (title,content) {
        if(!title){
            var title = '温馨提示';
        }
        if(!content){
            var content = title;
            title = "温馨提示";
        }
        sweetAlert(title, content, "error");
    }
    var sleep = function (second,callback) {
        if(typeof callback == 'function'){
            setTimeout(callback,second*1000);
        }
    }
    return {
        init: function () {
            //handleButtons();
        },
        alertError: function (title,content) {
            alertError(title,content);
        },
        confirm: function(title,subTitle,callback){
            handleConfirm(title,subTitle,callback);
        },
        sleep: function (second,callback) {
            sleep(second,callback);
        }
    }
}();

jQuery(document).ready(function () {
    Common.init();
})